home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / OTLLCTest / NegotiateRawModeSample.c next >
Encoding:
Text File  |  2000-09-28  |  7.4 KB  |  208 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        NegotiateRawModeSample.c
  3.  
  4.     Contains:    This sample demonstrates the use of Option Management to place an Ethernet
  5.                 endpoint into raw mode.  In addition, the sample distinguishes whether the
  6.                 ethernet driver is based on the original Ethernet template that was used on
  7.                 the Power Mac 72/73/75/76/85/86/95/9600 systems. Under SSW 8.5, the ethernet
  8.                 driver will be based on the Apple Ethernet template which is a modification 
  9.                 of the Mentat template customized to the Mac OS. For the purposes of
  10.                 enabling and using raw mode, the Apple Ethernet template can be considered
  11.                 the same as the mentat template.
  12.     
  13.                 BACKGROUND
  14.                 As of the release of OpenTransport 1.1.1, a new driver template, provided
  15.                 by Mentat, was released.  This new driver template is simpler to use and
  16.                 provides a standard mechanism for handling promiscuous mode.
  17.     
  18.                 There are 2 important differences between these two templates.  The mechanism
  19.                 by which an Ethernet endpoint is placed into raw mode is different.  In addition,
  20.                 the raw mode packet that the mentat based driver passes, must be handled
  21.                 differently from packets passed up by the original ethernet drivers.
  22.     
  23.                 OBJECTIVE:
  24.                 This code snippet has been modified to do 2 things
  25.                 1. demonstrate a method for placing an ethernet endpoint into raw mode regardless
  26.                 of the template type that the underlying driver is based on, and,
  27.                 2. to return the template type so that the caller will know how to correctly
  28.                 handle the raw mode packets.
  29.     
  30.                 FOR USERS OF PREVIOUS VERSIONS OF THIS CODE
  31.     
  32.                 IMPORTANT Note: This sample does not support asynchronous endpoints. The technique
  33.                 of checking whether the endpoint is async on entry to this function and changing
  34.                 it to sync, can work under some cases, however, this is not always true.
  35.                 If an async endpoint must be used, then modify the following code as appropriate
  36.                 to work with a handler routine that handles the T_OPTMGMTCOMPLETE event.  The cookie
  37.                 will be a pointer to the returned TOptMgmt structure so that you can check the 
  38.                 negotiation result.
  39.     
  40.                 Input parameters:
  41.                 EndpointRef - ethernet endpoint that you want to place into raw mode.
  42.                 rawModeOption - one of the following values
  43.                                 kOTRawRcvOn    - enable raw mode
  44.                                 kOTRawRcvOff - disable raw mode
  45.                     
  46.                                     // where the mentat template is present
  47.                                 kOTRawRcvOnWithTimeStamp - enable raw mode and return timestamp
  48.     
  49.                 return paramters:
  50.                 result - kOTNoError if the raw mode option was successfully negotiated
  51.                          < 0 OptionManagement call failed with the returned error
  52.                          > 0 OptionManagement call completed with no error, but the negotiation
  53.                                  returned a result other than T_SUCCESS
  54.                 templateType - type of ethernet template that was detected.
  55.                                 kUnknownTemplate
  56.                                 kOrigTemplate,
  57.                                 kMentatTemplate
  58.  
  59.                 raw mode data -
  60.                 An ethernet driver based on the original template will return the ethernet packet
  61.                 in the OTRcvUData buffer such that the destination address begins at offset 0 in
  62.                 the buffer.  A Mentat based driver will prepend the ethernet packet with the 
  63.                 following structure so that the destination packet begins at offset 0x18.
  64.     
  65.                 struct dl_recv_status_t {
  66.                 unsigned long    dl_overall_length;
  67.                 unsigned long    dl_flags;
  68.                 unsigned long    dl_packet_length_before_truncation;
  69.                 unsigned long    dl_pad;
  70.                 OTTimeStamp        dl_timestamp;
  71.                 };
  72.     
  73.                 This structure is defined in the file dlpiuser.h.  This header is found in the 
  74.                 OT Modules Development samples folder of the OT SDK.
  75.  
  76.     Written by: Rich Kubota    
  77.  
  78.     Copyright:    Copyright © 1998-1999 by Apple Computer, Inc., All Rights Reserved.
  79.  
  80.                 You may incorporate this Apple sample source code into your program(s) without
  81.                 restriction. This Apple sample source code has been provided "AS IS" and the
  82.                 responsibility for its operation is yours. You are not permitted to redistribute
  83.                 this Apple sample source code as "Apple sample source code" after having made
  84.                 changes. If you're going to re-distribute the source, we require that you make
  85.                 it clear in the source that the code was descended from Apple sample source
  86.                 code, but that you've made changes.
  87.  
  88.     Change History (most recent first):
  89.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  90.                 
  91.  
  92. */
  93.  
  94. #include <Gestalt.h>
  95. #include "NegotiateRawModeSample.h"
  96. #include "OpenTptLinks.h"
  97.  
  98. static Boolean    IsDesiredOTVersionPresent(UInt32 desVers);
  99.  
  100. // important note - use the options as defined in the OpenTptLinks.h header
  101. // when setting the rawModeOption parameter.
  102. pascal OSStatus DoNegotiateRawModeOption(EndpointRef ep, 
  103.                                         UInt32 rawModeOption, 
  104.                                         UInt32 *templateType)
  105.  
  106. {
  107.     UInt8        twelveByteOptionBuf[kOTOptionHeaderSize+12];
  108.     dl_recv_control_t *rawModePtr;
  109.     UInt8        buf[kOTFourByteOptionSize];    // define buffer for fourByte Option size
  110.     TOption*    opt;                        // option ptr to make items easier to access
  111.     TOptMgmt    req;
  112.     TOptMgmt    ret;
  113.     OSStatus    err;
  114.     
  115.     
  116.     *templateType = kUnknownTemplate;        // intialize the template type return value
  117.     
  118.     opt = (TOption*)buf;                    // set option ptr to buffer
  119.     req.opt.buf    = buf;
  120.     req.opt.len    = sizeof(buf);
  121.     req.flags    = T_NEGOTIATE;                // negotiate for rawmode option
  122.  
  123.     ret.opt.buf = buf;
  124.     ret.opt.maxlen = kOTFourByteOptionSize;    // first try the four-byte option request
  125.     
  126.  
  127.     opt->level    = LNK_TPI;                    // dealing with tpi
  128.     opt->name    = OPT_SETRAWMODE;
  129.     opt->len    = kOTFourByteOptionSize;
  130.     opt->status = 0;
  131.     *(UInt32*)opt->value = rawModeOption;        // set the desired option level, true or false
  132.  
  133.     if (OTIsSynchronous(ep) == false)            // check whether ep sync or not
  134.     {
  135.         DebugStr("\p DoNegotiateRawModeOption does not support async endpoints");
  136.         return (-1);
  137.     }
  138.                 
  139.     err = OTOptionManagement(ep, &req, &ret);
  140.         
  141.         // if no error then return the option status value
  142.     if (err == kOTNoError)
  143.     {
  144.         if (opt->status != T_SUCCESS)
  145.             err = opt->status;
  146.         else
  147.             *templateType = kOrigTemplate;    // the negotiation succeeded so we are dealing
  148.                                             // with a driver based on orig template
  149.     }
  150.     
  151.     if (err != kOTNoError)
  152.     {
  153.             // check if OT 1.2 or greater is present
  154.             // there is a bug in the tpi8022 module in OT <= v1.1.x, such that 12 byte raw modes
  155.             // options are not passed to the dlpi module and an IONACK result is returned.
  156.         if (IsDesiredOTVersionPresent(kOTVers12))
  157.         {
  158.                 // let's try the 12 byte option
  159.             opt = (TOption*)twelveByteOptionBuf;    // set option ptr to buffer
  160.             req.opt.buf    = twelveByteOptionBuf;
  161.             req.opt.len    = sizeof(twelveByteOptionBuf);
  162.             req.flags    = T_NEGOTIATE;                // negotiate for rawmode option
  163.  
  164.             ret.opt.buf = twelveByteOptionBuf;
  165.             ret.opt.maxlen = sizeof(twelveByteOptionBuf);
  166.  
  167.             opt->level    = LNK_TPI;                    // dealing with tpi
  168.             opt->name    = OPT_SETRAWMODE;
  169.             opt->len    = sizeof(twelveByteOptionBuf);
  170.             opt->status = 0;
  171.             
  172.             rawModePtr = (dl_recv_control_t*)&opt->value;
  173.             rawModePtr->dl_primitive = kOTSetRecvMode;
  174.             rawModePtr->dl_flags = DL_NORMAL_STATUS;
  175.             rawModePtr->dl_truncation_length = 0;
  176.             
  177.             err = OTOptionManagement(ep, &req, &ret);
  178.  
  179.                 // if no error then return the option status value
  180.             if (err == kOTNoError)
  181.             {
  182.                 if (opt->status != T_SUCCESS)
  183.                     err = opt->status;
  184.                 else
  185.                     *templateType = kMentatTemplate;
  186.             }
  187.             
  188.         }
  189.         
  190.     }
  191.  
  192.     return err;
  193. }
  194.  
  195. /*
  196. */
  197.  
  198. Boolean    IsDesiredOTVersionPresent(UInt32 desVers)
  199. {
  200.     OSErr    err;
  201.     UInt32    otVersion;
  202.     
  203.     err = Gestalt(gestaltOpenTptVersions, (long*) &otVersion);
  204.     if (err)
  205.         return false;
  206.     else
  207.         return (otVersion >= desVers);
  208. }